<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ page import="board1.BoardDAO"%>
<%@ page import="board1.BoardTO"%>
<%@ page import="board1.ReplyDAO"%>
<%@ page import="board1.ReplyTO"%>
<%@ page import="java.util.ArrayList"%>

<%
	request.setCharacterEncoding("UTF-8");

	int cpage = 1;
	if (request.getParameter("cpage") != null
			&& !request.getParameter("cpage").equals("")) {
		cpage = Integer.parseInt(request.getParameter("cpage"));
	}

	BoardDAO dao = new BoardDAO();

	ArrayList<BoardTO> data = dao.listBoard();

	String board_list = "";

	for (int i = 0; i < data.size(); i++) {
		BoardTO to = (BoardTO) data.get(i);
		String phone_number = to.getPhone_number();
		String chon_id = to.getChon_id();
		String title = to.getTitle();
		String content = to.getContent();
		String wr_dt = to.getWr_dt();
		String pic_names = to.getPic_name();
		String idx = to.getIdx();

		//댓글 json 문자열 시작 ----

		ReplyDAO rep_dao = new ReplyDAO();

		ArrayList<ReplyTO> rep_data = rep_dao.listReply(idx);

		String reply_list = "";

		for (int re_i = 0; re_i < rep_data.size(); re_i++) {
			ReplyTO rep_to = (ReplyTO) rep_data.get(re_i);
			String rep_phone_number = rep_to.getPhone_number();
			String rep_idx = to.getIdx();
			String subcontent = rep_to.getSubcontent();

			if (re_i == 0) {
				reply_list = "{\"phone_number\":\"" + rep_phone_number
						+ "\",\"subcontent\":\"" + subcontent + "\"}";

			} else {
				reply_list += ",{\"phone_number\":\"" + rep_phone_number
						+ "\",\"subcontent\":\"" + subcontent + "\"}";
			}
		}

		if (reply_list.equals(""))
			reply_list = "[\" Nothing (" + idx + ")\"]";
		else
			reply_list = "[" + reply_list + "]";

		out.println(reply_list);
		// 댓글 json문자영 끝---- 

		if (i == 0) {
			board_list = "{" + "\"phone_number\"" + ":\""
					+ phone_number + "\"," + "\" chon_id\"" + ":\""
					+ chon_id + "\"," + "\" title\"" + ":\"" + title
					+ "\"," + "\" content\"" + ":\"" + content + "\","
					+ "\" wr_dt\"" + ":\"" + wr_dt + "\","
					+ "\" pic_names\"" + ":\"" + pic_names + "\"}";
		} else {
			board_list += ",{" + "\"phone_number\"" + ":\""
					+ phone_number + "\"," + "\" chon_id\"" + ":\""
					+ chon_id + "\"," + "\" title\"" + ":\"" + title
					+ "\"," + "\" content\"" + ":\"" + content + "\","
					+ "\" wr_dt\"" + ":\"" + wr_dt + "\","
					+ "\" pic_names\"" + ":\"" + pic_names + "\"}";
		}

	}

	if (board_list.equals(""))
		board_list = "[\" Nothing \"]";
	else
		board_list = "[" + board_list + "]";

	out.println(board_list);
%>
